package com.woniu.cms.client.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.client.core.entity.ClientCompetitor;
import com.woniu.cms.client.core.entity.ClientInfo;
import com.woniu.cms.client.core.service.IClientCompetitorService;
import com.woniu.cms.client.core.service.IClientComplaintService;
import com.woniu.cms.client.core.vo.ClientCompetitorVo;
import com.woniu.cms.client.core.vo.ClientInfoVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 该表是竞争对手基本信息表，储存竞争对手名称和联系方式等固定信息 前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-23
 */


@RestController
@RequestMapping("/client-competitor")
public class ClientCompetitorController {

    @Autowired
    private IClientCompetitorService iClientCompetitorService;

    @ApiOperation("根据id删除客户信息")
    @DeleteMapping("/{id}")
    public Result deleteClientCompetitor(@PathVariable Integer id){

        boolean b = iClientCompetitorService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除竞争对手基本信息")
    @DeleteMapping
    public Result deleteClientCompetitor(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iClientCompetitorService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }

    @ApiOperation("修改竞争对手基本信息")
    @PutMapping
    public Result updateClientCompetitor(@RequestBody ClientCompetitor clientCompetitor){
        boolean save = iClientCompetitorService.updateById(clientCompetitor);
        return save ? Result.ok() :Result.error();
    }

    @ApiOperation("保存竞争对手基本信息")
    @PostMapping("savecc")
    public Result saveClientCompetitor(@RequestBody ClientCompetitor clientCompetitor){

        boolean save = iClientCompetitorService.save(clientCompetitor);
        return save ? Result.ok() :Result.error();
    }

    @ApiOperation("保存竞争对手基本信息")
    @PostMapping("saveByName")
    public Result saveClientCompetitorName(String bidcompetitorName){

        Result save = iClientCompetitorService.saveName(bidcompetitorName);

        return save;
    }


    @ApiOperation(value = "通过竞争对手基本信息列表Id查找竞争对手基本信息对象")
    @GetMapping("{id}")
    public  Result findById(@PathVariable   Integer id) {

        ClientCompetitor clientCompetitor = iClientCompetitorService.getById(id);

        return Result.common(clientCompetitor);
    }

    @ApiOperation(value = "通过客户姓名查找客户对象")
    @GetMapping("byname")
    public  Result findByName(String bidcompetitorName) {

        System.out.println("bidcompetitorName = " + bidcompetitorName);

        QueryWrapper<ClientCompetitor> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("bidcompetitor_name",bidcompetitorName);

        ClientCompetitor clientCompetitor = iClientCompetitorService.getOne(queryWrapper);

        return  Result.common(clientCompetitor);

    }


    @ApiOperation(value = "查询本次投标的竞争对手是否已经创建了客户的基本信息")
    @GetMapping("isname")
    public  void findIsByName(String bidcompetitorName) {

        System.out.println("bidcompetitorName = " + bidcompetitorName);

        QueryWrapper<ClientCompetitor> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("bidcompetitor_name",bidcompetitorName);

        ClientCompetitor clientCompetitor = iClientCompetitorService.getOne(queryWrapper);

        if (clientCompetitor == null){
            Result result = saveClientCompetitorName(bidcompetitorName);
        }else if (clientCompetitor!=null){
            return;
        }
    }

    @GetMapping("page")
    @ApiOperation(value = "竞争对手基本信息分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageClientCompetitor(Integer currentPage, Integer pageSize){

        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);


        IPage<ClientCompetitor> clientCompetitorIPage = new Page(currentPage,pageSize);

        IPage<ClientCompetitor> page = iClientCompetitorService.page(clientCompetitorIPage);

        System.out.println("page = " + page);

        List<ClientCompetitor> records = page.getRecords();

        System.out.println("records = " + records);

        long total = page.getTotal();

        System.out.println("total = " + total);


        List<ClientCompetitorVo> clientCompetitorVos = copyListProperties(records);



        Map<String,Object> map = new HashMap<>();
        map.put("data",clientCompetitorVos);
        map.put("total",total);

        return Result.common(map);


    }


    private List<ClientCompetitorVo>  copyListProperties(List<ClientCompetitor>  records){

        List<ClientCompetitorVo> clientCompetitorVos  = MyBeanUtils.copyListProperties(records, ClientCompetitorVo::new);

        return clientCompetitorVos;

    }


}

